package servlets;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class SaveFile
 */
public class SaveFile extends HttpServlet {
	private static final long serialVersionUID = 1L;

	// private static String serverRootPath = null;

	public void init(ServletConfig config) throws ServletException {
		// serverRootPath = config.getServletContext().getRealPath("/");
		// System.out.println("The root directory of the server is: " +
		// serverRootPath);
	}

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public SaveFile() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String filename = (String) request.getParameter("fn");
		String content = (String) request.getParameter("content");
		File bai_file = null;
		bai_file = new File(request.getSession().getServletContext()
				.getRealPath("/")
				+ "generated_files/" + filename);
//		System.out.println(bai_file.getAbsolutePath());
		OutputStreamWriter osw = null;
		try {
			osw = new OutputStreamWriter(new FileOutputStream(bai_file));
			osw.write(content);
			osw.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {

		} finally {
			try {
				osw.flush();
				osw.close();
			} catch (IOException e) {
				//
			}
		}

		PrintWriter out = response.getWriter();
		out.println("<html><head><title>Download File</title><style type='text/css'>.download {background: url(\"./img/download.jpg\") no-repeat scroll left top transparent;height:40px;width:135px;border:0px;}</style></head><body>");
		out.println("<a href='./'>Back to index</a>");
		out.println("<div style='margin-top:220px;text-align:center;'><p style='color:rgb(153,204,0);font-weight:bold;font-size:1.5em;'>Saved successfully!</p>");
		String params = "filename=" +java.net.URLEncoder.encode(bai_file.getName()) + "&path=" + java.net.URLEncoder.encode(bai_file.getAbsolutePath());
		//System.out.println("<button onClick=\"javascript:window.location='DownloadFile?" + params + "'\" class=\"download\" onmouseover=\"this.style.backgroundPosition='left -40px'\" onmouseout=\"this.style.backgroundPosition='left top'\" value=\"Download\" />");
		out.println("<button onClick=\"javascript:window.location='DownloadFile?" + params + "'\" class=\"download\" onmouseover=\"this.style.backgroundPosition='left -40px'\" onmouseout=\"this.style.backgroundPosition='left top'\" value=\"Download\" />");
		out.println("</div></body></html>");
		out.flush();
		out.close();
	}

}
